package com.weasel.weixin.mp;

import com.weasel.helper.ClassPathFileReader;
import com.weasel.weixin.commons.exception.UnSupportFileException;
import com.weasel.weixin.commons.message.GroupMessageSendResponse;
import com.weasel.weixin.commons.message.bean.group.Atricles;
import com.weasel.weixin.commons.message.bean.group.Group;
import com.weasel.weixin.commons.message.bean.group.Image;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;
import com.weasel.weixin.mp.request.MpGroupMessageSendRequest;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 群发 测试类
 * @author linhaibo
 * @time 2015年10月26日
 */
public class MpGroupMessageRequestDemo {
	
	public static void main(String[]args) throws Exception{
		
		MpConfig config=MpConfigBuilder.fromXML("classpath:weixin-config.xml");
		
		MpGroupMessageSendRequest request=new MpGroupMessageSendRequest(config);
		
		//uploadimg(request);
		uploadnews(request);
	}
	
	public static void uploadimg(MpGroupMessageSendRequest request) throws Exception{
		InputStream fileInputStream=ClassPathFileReader.readToInputStrream("classpath:mm.jpeg");
		File file=new File("media.jpeg");
		FileUtils.copyInputStreamToFile(fileInputStream, file);
		Group image=new Image();
		image.setFile(file);
		GroupMessageSendResponse response=request.uploadimg(image);
		if(!response.isOK()){
			throw new UnSupportFileException(response.getErrmsg());
		}
		System.out.println(response);
	}
	
	public static void uploadnews(MpGroupMessageSendRequest request) throws ExecutionException, IOException{
		Atricles atricle=new Atricles();
		atricle.setThumb_media_id("qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p");
		atricle.setAuthor("xxx");
		atricle.setTitle("Happy Day");
		atricle.setContent_source_url("www.qq.com");
		atricle.setContent("content");
		atricle.setDigest("digest");
		atricle.setShowCoverPic(true);
		
		List<Atricles> atricles=new ArrayList<Atricles>();
		atricles.add(atricle);
		
		GroupMessageSendResponse response=request.uploadnews(atricles);
		System.out.println(response);//40007 	 	不合法的媒体文件id  
	}
}
